<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;
use app\common\model\GoodsCategory;
class GoodsDonateLeaveWord extends Model
{

    use SoftDelete;

    

    // 表名
//    protected $name = 'GoodsDonate';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
    ];



    public function user(){
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function goodsuser(){
        return $this->belongsTo('User', 'goods_user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function goods(){
        return $this->belongsTo('GoodsDonate', 'goods_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    /**
     * 商品详情
     * */
    public static function getInfo($id){
        $where['goods_donate.id'] = $id;
        $info  = self::where($where)
            ->with(['category1','user','weight'])
            ->find();
        if(!$info) return false;
        $info->getRelation('user')->visible(['avatar','nickname','id']);
        $info->getRelation('category1')->visible(['id','name']);
        $info->getRelation('weight')->visible(['id','weight']);
        $info['cname'] = $info['category1']['name'];
        $info['user_avatar'] = $info['user']['avatar'];
        $info['user_nickname'] = $info['user']['nickname'];
        $info['user_id'] = $info['user']['id'];
        $set = get_set();
        $info->send =$set['conversion'];
        return $info;
    }
    /*
     * 获取留言
     * $goods_id 商品id
     * $user_id 留言用户id
     * */
    public static function getliu($goods_id,$user_id=''){

        $where['goods_donate_leave_word.goods_id'] =$goods_id;
        $where['goods_donate_leave_word.pid'] =0;
        if(!empty($user_id)){
            $where['goods_donate_leave_word.user_id'] =$user_id;
        }
        $list = self::with(['user'])
            ->where($where)
            ->select();

        foreach ($list as $k=>$v){
            $v->getRelation('user')->visible(['avatar','nickname','id']);
            $goodsuinfo =  self::with(['goodsuser'])
                ->where(['goods_donate_leave_word.pid'=>$v['id']])
                ->find();
            if(!empty($goodsuinfo)){
                $goodsuinfo->getRelation('goodsuser')->visible(['avatar','nickname','id']);
//            $goodsuinfo->visible(['avatar','nickname','id']);
                $goodsuinfo->createtime= datetime($goodsuinfo->createtime);
                $goodsuinfo->content= '回复@'.$v->user->nickname.':'.$goodsuinfo->content;
                $goodsuinfo = [$goodsuinfo];
            }
            $v->liu = [$goodsuinfo];

            $v->liu1 = $goodsuinfo;
            $v->createtime= datetime($v->createtime);

        }
        return $list;

    }
    /*
     * 留言新增
     * $goods_id商品id
     * $user_id 留言用户id
     * $content 留言内容
     * */
    public static function getAdd($goods_id,$user_id,$content,$pid=0){
        $where['status']=1;
        $where['state']=1;
        $where['id']=$goods_id;
        $GoodsDonate = GoodsDonate::where($where)->find();

        $data['goods_id'] = $GoodsDonate['id'];
        $data['goods_user_id'] = $GoodsDonate['uid'];
        $data['pid'] = $pid;
        $data['content'] = $content;
        $data['createtime']=time();
        $data['updatetime']=time();
        if(!empty($pid)){
            $self    = self::get($pid);
            $user_id = $self['user_id'];
            if($GoodsDonate['uid']!=$user_id){
//                return '捐赠者可回复';
            }
        }
        $data['user_id'] = $user_id;
        return self::create($data);
    }
}
